package com.rd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rd.mapper.EquipmentMapper;
import com.rd.pojo.Equipment;
import com.rd.service.IEquipmentService;
import com.rd.util.ResultModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author qiufenfen
 * @since 2023-10-28
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements IEquipmentService {
    @Resource
    EquipmentMapper equipmentMapper;

    @Override
    public ResultModel getEquipmentByPage(Integer currentPage, Integer size, Equipment equipment, LocalDate[] beginDateScope) {
        //开启分页
        Page<Equipment> page = new Page<>(currentPage, size);
        IPage<Equipment> equipmentByPage = equipmentMapper.getEquipmentByPage(page, equipment, beginDateScope);
        long total = equipmentByPage.getTotal();
        List<Equipment> records = equipmentByPage.getRecords();
        return ResultModel.createBySuccess().put("total", total).put("empList", records);
    }

    /**
     * @return
     * @description: 获取工号
     * @param:
     */
    @Override
    public ResultModel maxWorkID() {
        List<Map<String, Object>> maps = equipmentMapper.selectMaps(new QueryWrapper<Equipment>().select("max(workID)"));
        return ResultModel.createBySuccess("").put("maxWorkId",String.format("%08d", Integer.parseInt(maps.get(0).get("max(workID)").toString()) + 1));
    }

    /**
     * @return
     * @description: 添加员工
     * @param: Equipment
     */
    @Override
    public ResultModel addEmp(Equipment equipment) {
        if (1 == equipmentMapper.insert(equipment)) {
            return ResultModel.createBySuccess("添加成功！");
        }
        return ResultModel.createByErrorCodeMessage("添加失败！");
    }

    @Override
    public List<Equipment> getEquipment(Integer id) {
        return equipmentMapper.getEquipment(id);
    }



}
